﻿
function validateEmail(item) {
    var email = $(item).val();

   var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
   if (filter.test(email)) {
       $(item).css("border-color", "#A9A9A9");
       $(item).css("border-width", "1px");
   return true;
}
$(item).css("border-color", "red");
$(item).css("border-width", "1px");
   return false;

}
function validatePhone(item) {
    var result = true;
    var phone = $(item).val();
    if ($(item).val() == "") {
        result = false;
        
    } else {

        if (phone.length <= 10) {
            if (isNaN(phone)) {
                result = false;
                
            }
        } else {
            result = false;
            
        }
    }
    $(item).css("border-color", "black");
    $(item).css("border-width", "1px");
    if (result) {
        $(item).css("border-color", "black");
        $(item).css("border-width", "1px");
    } else {
        $(item).css("border-color", "red");
        $(item).css("border-width", "1px");
    }


return result;

}
function Empty(item) {
    if ($(item).val().trim() == "") {
        $(item).css("border-color", "red");
        return false;
    }
    $(item).css("border-color", '#A9A9A9');
    $(item).css("border-width", "1px");
    return true;
}
function checkRange(min, max, item) {
    $(item).css("border-width", "1px");
    var value = $(item).val();
    if (value.trim().length <= max && value.trim().length >= min) {
        $(item).css("border-color", "#A9A9A9");
   return true;
}else
$(item).css("border-color", "red");
 return false;
}

function checkDate(item) {
    // format D(D)/M(M)/(YY)YY
    var allowBlank = true;
    var minYear = 1902;
    var maxYear = (new Date()).getFullYear();
    var value = $(item).val();  
    var errorMsg = "";

    // regular expression to match required date format
    re = /^(\d{4})-(\d{1,2})-(\d{1,2})$/;

    if (value != '') {
        if (regs = value.match(re)) {
            if (regs[3] < 1 || regs[3] > 31) {
                errorMsg = "Invalid value for day: " + regs[1];
            } else if (regs[2] < 1 || regs[2] > 12) {
                errorMsg = "Invalid value for month: " + regs[2];
            } else if (regs[1] < minYear || regs[1] > maxYear) {
                errorMsg = "Invalid value for year: " + regs[1] + " - must be between " + minYear + " and " + maxYear;
            }
        } else {
            errorMsg = "Invalid date format: " + value;
        }
    } else  {
        errorMsg = "Empty date not allowed!";
    }

    if (errorMsg != "") {
        //alert(errorMsg);
        //field.focus();
        $(item).css("border-color", "red");
        $(item).css("border-width", "1px");
        return false;
    }
    $(item).css("border-color", "black");
    $(item).css("border-width", "1px");
    return true;
}


